"""
<name>Survival Fit</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description>Generates a survival fit of either a data table or a fit model.  This widget can be connected to a plotting widget for generating survival curves.</description>
<RFunctions>survival:survfit</RFunctions>
<tags>Survival</tags>
<icon>survival.png</icon>
<inputWidgets></inputWidgets>
<outputWidgets>survival_survivalPlot</outputWidgets>
"""
from OWRpy import * 
import redRGUI, signals
class survfit(OWRpy): 
    def __init__(self, **kwargs):
        OWRpy.__init__(self, wantGUIDialog = 1, **kwargs)
        
        self.setRvariableNames(["survfit"])
        self.data = {}
        self.RFunctionParam_data = ''
        self.inputs.addInput("data", 'Data Table', signals.base.RVariable, self.processdata)
        self.inputs.addInput('id1', 'Model Fit', signals.base.RModelFit, self.processfit)
        self.outputs.addOutput("survfit Output", 'Survival Fit', signals.survival.RSurvFit)
        
        #self.help.setHtml('<small>Generates a survival fit to a set of data.  This can be used in plotting.</small>')
        formulaBox = redRGUI.base.widgetBox(self.controlArea)
        self.times = redRGUI.base.comboBox(formulaBox, label = 'Times', toolTip = 'The event times.')
        self.event = redRGUI.base.comboBox(formulaBox, label = 'Events', toolTip = 'The event status. 1 means an event occurred\nwhile 0 means there was no event at the end time.')
        self.groupings = redRGUI.base.comboBox(formulaBox, label = 'Groupings', toolTip = 'The column that specifies the groupings of the data.\nThis is optional.')
        self.RFunctionParamweights_lineEdit =  redRGUI.base.lineEdit(self.GUIDialog,  label = "weights:", text = '', toolTip = 'The weights applied to the data, should be in the form c(weight1, weight2, ...).')
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processfit(self, data):
        if not self.require_librarys(['survival']):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.times.clear()
            self.event.clear()
            self.groupings.clear()
            self.data = data
            self.R(self.Rvariables['survfit']+'<-survfit('+data.getData()+')', wantType = 'NoConversion')
            self.out = signals.RModelFit(data=self.Rvariables["survfit"])
            self.rSend("survfit Output", self.out)
    def processdata(self, data):
        if not self.require_librarys(["survival"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_data=data.getData()
            colnames = self.R('colnames('+self.RFunctionParam_data+')')
            self.groupings.update([''] + colnames)
            self.times.update(colnames)
            self.event.update(colnames)
            self.data = data
            self.commitFunction()
    def commitFunction(self):
        if str(self.RFunctionParam_data) == '': return
        if str(self.times.currentText()) == str(self.event.currentText()): return
        if str(self.times.currentText()) == '' or str(self.event.currentText()) == '': return
        injection = []
        formula = 'formula = Surv('+str(self.times.currentText())+','+str(self.event.currentText())+')'
        if str(self.groupings.currentText()) != '':
            formula += '~'+str(self.groupings.currentText())
        else:
            formula += '~ 1'
        injection.append(formula)
        self.formula = formula
        if str(self.RFunctionParamweights_lineEdit.text()) != '':
            string = 'weights='+str(self.RFunctionParamweights_lineEdit.text())
            injection.append(string)
        inj = ','.join(injection)
        self.R(self.Rvariables['survfit']+'<-survfit(data='+str(self.RFunctionParam_data)+','+inj+')', wantType = 'NoConversion')
        self.out = signals.survival.RSurvFit(self, data=self.Rvariables["survfit"])
        self.rSend("survfit Output", self.out)

